<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="人才姓名" prop="talentName">
        <el-input
          v-model="dataForm.talentName"
          placeholder="人才姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
      </el-form-item>
      <el-form-item label="入选年份" prop="selectedYear">
        <el-input
          v-model="dataForm.selectedYear"
          placeholder="入选年份"
        ></el-input>
      </el-form-item>
      <el-form-item label="工作单位" prop="workUnit">
        <el-input v-model="dataForm.workUnit" placeholder="工作单位"></el-input>
      </el-form-item>
      <el-form-item label="申报企业" prop="requestCompany">
        <el-input
          v-model="dataForm.requestCompany"
          placeholder="申报企业"
        ></el-input>
      </el-form-item>
      <!-- <el-form-item label="图片" prop="imageUrl">
        <el-input v-model="dataForm.imageUrl" placeholder="图片"></el-input>
      </el-form-item> -->
      <!-- <el-form-item   label="图片路径" prop="imageUrl">
        <el-upload
           v-model="dataForm.imageUrl"
          class="upload-demo"
          ref="upload"
          :http-request = "customUpload"
          drag
          action=""
          multiple
          >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            将文件拖到此处，或
            <em>点击上传</em>
          </div>
          <div class="el-upload__tip" slot="tip">文件大小不超过50M</div>
        </el-upload>
      </el-form-item>  -->
      <el-form-item label="企业所在地址" prop="companyAdress">
        <el-input
          v-model="dataForm.companyAdress"
          placeholder="企业所在地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="人才类型" prop="talentTypeId">
        <el-input
          v-model="dataForm.talentTypeId"
          placeholder="人才类型"
        ></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
      </el-form-item>
      <el-form-item label="图片id" prop="imageId">
        <el-input v-model="dataForm.imageId" placeholder="图片id"></el-input>
      </el-form-item>
      <el-form-item label="事务id" prop="affirsInformationId">
        <el-input
          v-model="dataForm.affirsInformationId"
          placeholder="事务id"
        ></el-input>
      </el-form-item>

      <!-- <el-form-item   label="二维码" prop="code">
        <el-upload
           v-model="dataForm.code"
          class="upload-demo"
          ref="upload"
          :http-request = "customUpload"
          drag
          action=""
          multiple
          >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            将文件拖到此处，或
            <em>点击上传</em>
          </div>
          <div class="el-upload__tip" slot="tip">文件大小不超过50M</div>
        </el-upload>
      </el-form-item>  -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { fileUpload } from "@/utils";
//import QRCode from 'qrcode'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        talentName: "",
        sex: "",
        selectedYear: "",
        workUnit: "",
        requestCompany: "",
        companyAdress: [],
        talentTypeId: "",
        age: "",
        imageId: "",
        affirsInformationId: " "
      },
      dataRule: {
        talentName: [
          { required: true, message: "人才姓名不能为空", trigger: "blur" }
        ],
        sex: [{ required: true, message: "性别不能为空", trigger: "blur" }],
        selectedYear: [{ required: true, message: "入选年份不能为空", trigger: "blur" }],
        workUnit: [{ required: true, message: "工作单位不能为空", trigger: "blur" }],
       requestCompany: [{ required: true, message: "申报企业不能为空", trigger: "blur" }],
        companyAdress: [
          { required: true, message: "企业所在地址不能为空", trigger: "blur" }],
       talentTypeId: [{ required: true, message: "人才类型不能为空", trigger: "blur" }],
        age: [
          { required: true, message: "年龄不能为空", trigger: "blur" }],
      imageId: [{ required: true, message: "图片id不能为空", trigger: "blur" }],
        affirsInformationId: [
          { required: true, message: "事务id不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    customUpload(imageUrl) {
      fileUpload(imageUrl).then(response => {
        this.dataForm.imageUrl.push(response.data.url);
      });
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/rencai/talentability/delete`),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.talentName = data.supply.talentTypeId;
              this.dataForm.sex = data.supply.sex;
              this.dataForm.selectedYear = data.supply.selectedYear;
              this.dataForm.workUnit = data.supply.workUnit;
              this.dataForm.requestCompany = data.supply.requestCompany;
              this.dataForm.companyAdress = data.supply.companyAdress;
              this.dataForm.talentTypeId = data.supply.talentTypeId;
              this.dataForm.age = data.supply.age;
              this.dataForm.imageId=data.supply.imageId;
              this.dataForm.affirsInformationId=data.supply.affirsInformationId;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/rencai/talentability/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              talentName:this.dataForm.talentName ,
              sex:this.dataForm.sex,
              selectedYear:this.dataForm.selectedYear,
              workUnit:this.dataForm.workUnit,
              requestCompany:this.dataForm.requestCompany,
              companyAdress:this.dataForm.companyAdress,
              talentTypeId:this.dataForm.talentTypeId,
              age:this.dataForm.age,
              imageId:this.dataForm.imageId,
              affirsInformationId:this.dataForm.affirsInformationId,
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",

                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
